package cn.qihua.action;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import cn.qihua.model.SysUser;
import cn.qihua.service.IUserService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 米修
 * 
 */

public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3304239783129613128L;
	public String message;
	private boolean success = false;

	private int id;
	private Integer uid;
	private Integer organizationId;
	private String name;
	private String username;
	private String password;
	private String oldPwd;
	private String phone;
	private String qq;
	private String hi;
	private String email;
	private SysUser user = new SysUser();
	private IUserService userService;

	public String findOfMe() {
		this.getUid();
		if (uid == null) {
			this.setSuccess(false);
			return ERROR;
		}
		this.setUser(userService.findById(uid));
		if (user != null) {
			this.setEmail(user.getEmail());
			this.setHi(user.getHi());
			this.setOrganizationId(user.getOrganizationId());
			this.setPhone(user.getPhone());
			this.setName(user.getName());
			this.setQq(user.getQq());
			this.setMessage("资料加载完成！");
			this.setSuccess(true);
			return SUCCESS;
		}
		this.setSuccess(false);
		return ERROR;
	}

	public String updateInfo() {
		this.getUid();
		if (uid == null) {
			this.setMessage("登录超时，请重新登录！");
			this.setSuccess(false);
			return ERROR;
		}
		this.setUser(userService.findById(uid));
		if (user == null) {
			this.setMessage("参数错误！");
			this.setSuccess(false);
			return ERROR;
		}
		user.setEmail(email);
		user.setName(name);
		user.setOrganizationId(organizationId);
		user.setPhone(phone);
		user.setHi(hi);
		user.setQq(qq);
		if (userService.update(user)) {
			this.setMessage("资料保存成功！");
			this.setSuccess(true);
			return SUCCESS;
		}
		this.setMessage("资料保存失败！");
		this.setSuccess(false);
		return ERROR;
	}

	public String changePassword() {
		this.getUid();
		if (this.uid == null) {
			this.setMessage("登录超时，请重新登录！");
			this.setSuccess(false);
			return ERROR;
		}
		if (user != null) {
			if (userService.changePassword(uid, oldPwd, password)) {
				this.setMessage("修改密码成功！");
				this.setSuccess(true);
				return SUCCESS;
			}
		}
		this.setMessage("修改密码失败！");
		this.setSuccess(false);
		return ERROR;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public void setId(int id) {
		this.id = id;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@JSON(name = "data")
	public SysUser getUser() {
		return user;
	}

	public void setUser(SysUser user) {
		this.user = user;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setOrganizationId(Integer organizationId) {
		this.organizationId = organizationId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	public void setHi(String hi) {
		this.hi = hi;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public int getUid() {
		Subject subject = SecurityUtils.getSubject();
		this.uid = (Integer) subject.getSession().getAttribute("UID");
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public Integer getOrganizationId() {
		return organizationId;
	}

	public String getName() {
		return name;
	}

	public String getPhone() {
		return phone;
	}

	public String getQq() {
		return qq;
	}

	public String getHi() {
		return hi;
	}

	public String getEmail() {
		return email;
	}
}
